<?php
/**
 * Created by PhpStorm.
 * User: deng
 * Date: 2017/2/10
 * Time: 16:47
 */
class DiaryReviewService extends BaseService {

    const REVIEW_STATUS_IMG = 1;//审核中
    const REVIEW_STATUS_PASS = 0;//通过
    const REVIEW_STATUS_NOTPASS = 2;//不通过
    const REVIEW_STATUS_MANAGER_HANDLE = 3;//人工处理

    /**
     * 审核通过
     * @param $bodyId
     * @param int $handle_way 审核方式,0-无,1-自动,2-人工
     */
    function reviewPass($bodyId, $handle_way = 1){
        $logs = 'reviewDiaryPass.log';
        $diaryVideoModel = new DiaryVideoModel();
        $diaryListModel = new DiaryListModel();
        $diaryBodyModel = new DiaryBodyModel();
        $diaryCacheModel = new DiaryListCacheModel();
        $diaryController = new DiaryController();
        $userModel = new AutoUidModel();
        $review_info = $diaryVideoModel->getDiaryVideoInfoByBodyid($bodyId);
        UK::saveLog($logs, date('Y-m-d H:i:s').':$bodyId:'.print_r($bodyId, true)."\n");
        UK::saveLog($logs, date('Y-m-d H:i:s').':$review_info:'.print_r($review_info, true)."\n");
        if (empty($review_info) || !in_array($review_info['rate'], [ self::REVIEW_STATUS_IMG, self::REVIEW_STATUS_MANAGER_HANDLE ]))
            return ;
        $uid = $review_info['uid'];
        $list_info = $diaryListModel->getDiaryListInfoByBodyId($bodyId);
        $body_info = $diaryBodyModel->getDiaryBody($uid, $bodyId);
        if (empty($list_info) || empty($body_info))
            return ;
        $set = [
            'inReview' => 0,
            'uid' => $uid,
            'bodyId' => $bodyId,
            'time' => AC::SERVER_TIME,
            'saveTime' => AC::SERVER_TIME
        ];
        $diaryListModel->setDiaryList($set);
        $diaryBodyModel->setDiaryBody($set);
        if ($list_info['secret'] == 0){
            $diaryCacheModel->addUserTimeline($uid, $bodyId);
            UK::saveLog($logs, date('Y-m-d H:i:s').':addUserTimeline:'.print_r('ok', true)."\n");
            if ($geo = json_decode($list_info['geo'], true)){
                $diaryController->addDiaryLbs($geo, $bodyId, $uid);
                UK::saveLog($logs, date('Y-m-d H:i:s').':addDiaryLbs:'.print_r('ok', true)."\n");
            }
            if ($topics = json_decode($list_info['topics'], true)){
                $diaryInfo = [
                    'topic_id' => $topics['gid'],
                    'uid' => $uid,
                    'bodyId' => $bodyId,
                    'title' => $list_info['title'],
                    'content' => $body_info['content']
                ];
                $diaryController->sendDiaryToTopic($topics['gid'], ['gid'=>$topics['gid']], $diaryInfo);
                UK::saveLog($logs, date('Y-m-d H:i:s').':sendDiaryToTopic:'.print_r('ok', true)."\n");
            }
        }
        $member = $userModel->getUserInfo($uid);
        $feedInfo = [
            'uid'               =>  $uid,
            'eUid'              =>  $uid,
            'eNickname'         =>  $member['nickname'],
            'authorUid'         =>  $uid,
            'authorNickname'    =>  $member['nickname'],
            'bodyId'            =>  $bodyId,
            'type'              =>  AC::FEED_DIARY,
            'label'          =>   (isset($list_info['label']) && intval($list_info['label']) > 0 && intval($list_info['label']) <= AC::DIARY_TAG_MAXID) ? intval($list_info['label']) : 0
        ];
        strlen($body_info['content']) > 0    &&  $feedInfo['content']    =   $body_info['content'];
        strlen($list_info['title']) > 0      &&  $feedInfo['content']    =   $list_info['title'];
        strlen($body_info['content']) < 1 &&  $feedInfo['content']    =   ' write Diary, No title ? No content ? ! ';
        if ($list_info['rBodyId']>0 && $list_info['rUid']>0){
            $feedInfo['type'] = AC::FEED_REPOST;
        }
        UK::saveLog($logs, date('Y-m-d H:i:s').':$feedInfo:'.print_r($feedInfo, true)."\n");
        $feedController = new FeedController();
        $list_info['secret'] == 0 && $feedController->postFeed($feedInfo);
        UK::saveLog($logs, date('Y-m-d H:i:s').':postFeed:'.print_r('ok', true)."\n");
        $hotSquareController = new HotSquareController();
        $list_info['secret'] == 0 && $hotSquareController->postSquare($feedInfo);
        UK::saveLog($logs, date('Y-m-d H:i:s').':postSquare:'.print_r('ok', true)."\n");

        //更新审核状态
        $diaryCacheModel->delDiaryCache($bodyId);
        $set = ['rate'=>self::REVIEW_STATUS_PASS, 'operate'=>$handle_way, 'operate_at'=>AC::SERVER_TIME];
        $diaryVideoModel->updateDiaryVideoInfo($bodyId, $set);
        UK::saveLog($logs, date('Y-m-d H:i:s').':updateDiaryVideoInfo:'.print_r('ok', true)."\n");
    }

    /**
     * 审核不通过
     * @param $bodyId
     * @param int $handle_way 审核方式,0-无,1-自动,2-人工
     */
    public function reviewNotPass($bodyId, $handle_way = 1){
        $logs = 'reviewDiaryNotPass.log';
        $diaryVideoModel = new DiaryVideoModel();
        $diaryListModel = new DiaryListModel();
        $diaryController = new DiaryController();
        $review_info = $diaryVideoModel->getDiaryVideoInfoByBodyid($bodyId);
        UK::saveLog($logs, date('Y-m-d H:i:s').':$review_info:'.print_r($review_info, true)."\n");
        if (empty($review_info) || $review_info['rate']!=1)
            return ;
        if ($handle_way == 1){
            //自动审核不通过，转为人工待处理任务中
            $set = ['rate'=>self::REVIEW_STATUS_MANAGER_HANDLE, 'operate_at'=>AC::SERVER_TIME];
            $diaryVideoModel->updateDiaryVideoInfo($bodyId, $set);
            return;
        }
        $uid = $review_info['uid'];
        $list_info = $diaryListModel->getDiaryInfoByBodyId($bodyId);
        if (empty($list_info))
            return ;
        $param = [
            'uid' => $uid,
            'bodyId' => $bodyId,
            'secret' => $list_info['secret']
        ];
        $diaryController->removeDiary($param);
        if (is_array(AC::$userStatistics)){
            $useStatisticsController = new UserStatisticsController();
            foreach (AC::$userStatistics    as  $uid => $statistics)
            {
                if (empty($uid) ||  empty($statistics)) continue;
                $statistics['uid']  =   $uid;
                AC::$errorNo = null;
                $useStatisticsController->userStatistics($statistics);
            }
        }
        UK::saveLog($logs, date('Y-m-d H:i:s').':removeDiary:'.print_r('ok', true)."\n");
        //更新审核状态
        $extra = json_encode([
            'videoList' => $list_info['videoList']
        ]);
        $set = ['rate'=>self::REVIEW_STATUS_NOTPASS, 'operate'=>$handle_way, 'operate_at'=>AC::SERVER_TIME, 'extra'=>$extra ];
        $diaryVideoModel->updateDiaryVideoInfo($bodyId, $set);
        UK::saveLog($logs, date('Y-m-d H:i:s').':updateDiaryVideoInfo:'.print_r('ok', true)."\n");
        $messageService = new MessageService();
        $userModel = new AutoUidModel();
        $member = $userModel->getUserInfo($uid);
        $messageService->sendMessage(1, $uid, ['mNickname'=>$member['nickname'], 'content'=>'您上传的视频未通过审核，请遵守粉粉社区公约相关规定，禁止上传包含色情、违法、侵权等性质内容的视频。']);
        UK::saveLog($logs, date('Y-m-d H:i:s').':sendMessage:'.print_r('ok', true)."\n\n\n");
    }

}